package com.example.springdata_repositories.service.impl;

import cn.hutool.json.JSONUtil;
import com.example.springdata_repositories.domain.Students;
import com.example.springdata_repositories.repository.StudentsRepository;
import com.example.springdata_repositories.service.StudentsService;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

@Service
public class StudentsServiceImpl implements StudentsService {


    @Resource
    private RestHighLevelClient restHighLevelClient;

    @Override
    public Students saveStudents(Students s) throws Exception {
        try {
            IndexRequest indexRequest = new IndexRequest("students");
            indexRequest.id(s.getSid());
            //咱们使用hutool工具类把一个java对象转成json字符串。
            indexRequest.source(JSONUtil.toJsonStr(s), XContentType.JSON);
            indexRequest.timeout("1s");
            IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

            return s;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    @Override
    public Students queryStudentsBySid(String sid) {

        GetRequest request = new GetRequest("students", sid);
        try {
            GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
            Map<String, Object> result = response.getSource();
            Students s = JSONUtil.toBean(JSONUtil.toJsonStr(result), Students.class);
            //System.out.println(m);
            return s;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public Students updateStudents(Students s) throws Exception {
        IndexRequest indexRequest = null;
        IndexResponse indexResponse = null;
        try {
            indexRequest = new IndexRequest("students", "_doc", s.getSid());

            String jsonStr = JSONUtil.toJsonStr(s);
            indexRequest.source(jsonStr, XContentType.JSON);
            indexResponse = null;
            indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean removeStudents(String sid) throws Exception {
        DeleteRequest request = new DeleteRequest("students", sid);
        try {
            DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
            //System.out.println(response.getResult());
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
}
